package com.sunagy.demand.backend.service.impl;

import com.sunagy.demand.backend.VO.request.CustomerProgressAddRequestVO;
import com.sunagy.demand.backend.entity.CustomerProgress;
import com.sunagy.demand.backend.VO.response.CustomerProgressResponseVO;
import com.sunagy.demand.backend.repository.CustomerProgressRepository;
import com.sunagy.demand.backend.service.CustomerProgressService;
import com.sunagy.demand.backend.utils.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/26
 * time：上午11:44
 */
@Service
public class CustomerProgressServiceImpl implements CustomerProgressService {

    @Autowired
    private CustomerProgressRepository customerProgressRepository;

    @Override
    public List<CustomerProgressResponseVO> getAll() {
        List<CustomerProgress> customerProgressList = customerProgressRepository.findAllByEnable(true);
        List<CustomerProgressResponseVO> customerProgressResponseVOList = (List<CustomerProgressResponseVO>) BeanUtil.copyPropertiesList(customerProgressList,CustomerProgressResponseVO.class);
        return customerProgressResponseVOList;
    }

    @Override
    public List<CustomerProgressResponseVO> getEnableListByCustomerId(Integer customerId) {
        List<CustomerProgress> customerProgressList = customerProgressRepository.findAllByEnableAndUserid(true,customerId);
        List<CustomerProgressResponseVO> customerProgressResponseVOList = (List<CustomerProgressResponseVO>) BeanUtil.copyPropertiesList(customerProgressList,CustomerProgressResponseVO.class);
        return customerProgressResponseVOList;
    }

    @Override
    @Transactional
    public void dealProgress(Integer id) {
        CustomerProgress customerProgress = customerProgressRepository.findOne(id);
        customerProgress.setStatus(1);
        customerProgressRepository.save(customerProgress);
    }

    @Override
    @Transactional
    public CustomerProgressResponseVO add(CustomerProgressAddRequestVO customerProgressAddRequestVO) {
        CustomerProgressResponseVO customerProgressResponseVO = new CustomerProgressResponseVO();
        customerProgressAddRequestVO.setDic_name(customerProgressAddRequestVO.getProgressName());
        CustomerProgress customerProgress = new CustomerProgress();
        BeanUtil.copyProperties(customerProgress,customerProgressAddRequestVO);
        BeanUtils.copyProperties(customerProgressRepository.save(customerProgress), customerProgressResponseVO);
        return customerProgressResponseVO;
    }

    @Override
    @Transactional
    public CustomerProgressResponseVO changeStatus(Integer id) {
        CustomerProgressResponseVO customerProgressResponseVO = new CustomerProgressResponseVO();
        CustomerProgress customerProgress = customerProgressRepository.findOne(id);
        customerProgress.setStatus(1);
        BeanUtils.copyProperties(customerProgressRepository.save(customerProgress), customerProgressResponseVO);
        return customerProgressResponseVO;
    }

    @Override
    @Transactional
    public CustomerProgressResponseVO deleteById(Integer id) {
        CustomerProgressResponseVO customerProgressResponseVO = new CustomerProgressResponseVO();
        CustomerProgress customerProgress = customerProgressRepository.findOne(id);
        customerProgress.setEnable(false);
        BeanUtils.copyProperties(customerProgressRepository.save(customerProgress), customerProgressResponseVO);
        return customerProgressResponseVO;
    }

    @Override
    @Transactional
    public CustomerProgressResponseVO setDealTime(Integer id, Date date) {
        CustomerProgressResponseVO customerProgressResponseVO = new CustomerProgressResponseVO();
        CustomerProgress customerProgress = customerProgressRepository.findOne(id);
        customerProgress.setDeal_time(date);
        BeanUtils.copyProperties(customerProgressRepository.save(customerProgress), customerProgressResponseVO);
        return customerProgressResponseVO;
    }


}
